const getMinifyPicColor = (url = 'Hello.png') =>
  new Promise(resolve => {
    const canvas = document.createElement('canvas')
    const minify2DArr = []
    if (canvas) {
      const ctx = canvas.getContext('2d')
      if (ctx) {
        const img = new Image()
        img.src = '/images/' + url
        img.addEventListener('load', () => {
          // (图片对象, 图像裁剪的x位置, 图像裁剪的y位置, 裁剪的宽度, 裁剪的高度, x位置, y位置, 宽度, 高度)
          ctx.drawImage(img, 0, 0, 20, 10, 0, 0, 20, 10)

          // 获取像素数据
          const imageData = ctx.getImageData(0, 0, 20, 10)
          const pixels = imageData.data
          // console.log(pixels.length)

          for (let i = 0; i < pixels.length; i += 4) {
            minify2DArr.push({
              r: pixels[i],
              g: pixels[i + 1],
              b: pixels[i + 2],
            })
          }
          ctx.putImageData(imageData, 0, 0)
          // console.log(minify2DArr)
          const newArr = []
          while (minify2DArr.length) newArr.push(minify2DArr.splice(0, 20))
          // console.log(newArr)
          resolve(newArr)
        })
      }
    }
  })

export default getMinifyPicColor
